"""更新管理员密码"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))

from app.core.security import get_password_hash
import psycopg2
import os
from dotenv import load_dotenv

# Load .env
env_path = Path(__file__).parent.parent / '.env'
load_dotenv(env_path)

DB_HOST = os.getenv("DATABASE_HOST", "localhost")
DB_PORT = int(os.getenv("DATABASE_PORT", "5432"))
DB_USER = os.getenv("DATABASE_USER", "postgres")
DB_PASSWORD = os.getenv("DATABASE_PASSWORD", "")
DB_NAME = os.getenv("DATABASE_NAME", "shihe_knowledgehub")

print("Updating admin password...")

# Generate new hash
new_hash = get_password_hash("admin123")
print(f"New hash: {new_hash}")

try:
    conn = psycopg2.connect(
        host=DB_HOST,
        port=DB_PORT,
        user=DB_USER,
        password=DB_PASSWORD,
        database=DB_NAME
    )
    cursor = conn.cursor()
    
    # Update password
    cursor.execute("UPDATE users SET hashed_password = %s WHERE username = 'admin'", (new_hash,))
    conn.commit()
    
    print("[SUCCESS] Admin password updated!")
    print("Username: admin")
    print("Password: admin123")
    
    cursor.close()
    conn.close()
    
except Exception as e:
    print(f"[ERROR] {e}")
    import traceback
    traceback.print_exc()

